package sms;

import java.time.LocalDate;

public class StudentManagementSystem {
    /*定义一个学生类型的数组*/
    private Student[] students;

    public StudentManagementSystem(int capacity){
        //capacity代表数组容量
        //传递一个整数，代表开辟数组的空间
        this.students = new Student[capacity];
    }

    /**
     * 如果返回值有多个,我们可以考虑将多个返回值封装到一个容器中
     * 然后返回该容器
     * @param date 指定日期
     * @return 装满了满足条件的学生信息数组
     */
    public Student[] getStudentsEnrolledBefore(String date){
        //1.定义一个数组来存放满足条件的学生
        Student[] temps = new Student[students.length];

        //2.遍历students中的每个学生,逐个判断
        //如果该学生的入学日期在指定日期之前,我们就将该学生存入temps数组中
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.getEnrollmentDates() != null) {
                if (student.getEnrollmentDates().isBefore(LocalDate.parse(date)) || student.getEnrollmentDates().isEqual(LocalDate.parse(date))) {
                    addStudent(temps, student);
                }
            }
        }
        return temps;
    }

    /**
     * 学生信息管理系统能够查询指定日期之后(包含指定日期)入学的学生信息
     * @param date 指定的日期
     * @return 装满了满足条件的学生信息数组
     */
    public Student[] getStudentsEnrolledAfter(String date){
        //1.定义一个数组来存放满足条件的学生
        Student[] temps = new Student[students.length];

        //2.遍历students中的每个学生,逐个判断
        //如果该学生的入学日期在指定日期之前,我们就将该学生存入temps数组中
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.getEnrollmentDates() != null) {
                if (student.getEnrollmentDates().isAfter(LocalDate.parse(date)) || student.getEnrollmentDates().isEqual(LocalDate.parse(date))) {
                    addStudent(temps, student);
            }
        }                }

        return temps;
    }

    public void addStudent(Student[] students,Student student){
        for(int i = 0;i< students.length;i++){
            if(students[i]==null){//如果数组中某个位置为null，则可以存入学生
                students[i] = student;
                break;//存入后则可以直接中断循环
            }
        }
    }

    /*接受学生对象(小学生或中学生)，对象中封装着学生的基本信息*/
    public void addStudent(Student student){
        for(int i = 0;i< students.length;i++){
            if(students[i]==null){//如果数组中某个位置为null，则可以存入学生
                students[i] = student;
                break;//存入后则可以直接中断循环
            }
        }
    }


    public boolean contains(Student stu){
        if (stu == null) {
            return false;
        }
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (stu != null && student.equals(stu)) {
                return true;
            }
        }
        return false;
    }

    public void showAllStudents(Student[] students){
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (students[i] != null) {//如果数组中取出的不是null，在进行信息展示，防止空指针异常
                System.out.println((i + 1) + " " + student.toString());
            }
        }
    }

    /*展示所有学生的信息*/
    public void showAllStudents(){
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (students[i] != null) {//如果数组中取出的不是null，在进行信息展示，防止空指针异常
                System.out.println((i + 1) + " " + student.toString());
            }
        }
    }

    /**
     *
     * @param subject 指定科目,例如: "语文"
     * @return 指定科目最高成绩的学生信息
     */

    public Student findStudentWithHighestScore(String subject){
        int highestscore = 0;
        Student studentWithHighestScore = null;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (students[i] != null) {
                int score = getScoreBySubject(student,subject);//输入科目寻找最高分
                if (score > highestscore) {
                    highestscore = score;
                    studentWithHighestScore = student;
                }
            }
        }
    return studentWithHighestScore;
    }

    public Student findStudentWithLowestScore(String subject){
        int lowestscore = Integer.MAX_VALUE;
        Student studentWithLowestScore = null;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (students[i] != null) {
                int score = getScoreBySubject(student,subject);//输入成绩寻找最低分
                if (score < lowestscore) {
                    lowestscore = score;
                    studentWithLowestScore = student;
                }
            }
        }
        return studentWithLowestScore;
    }

    public int getAverageScoreBySubject(String subject){
        int totalScore = 0;
        int count = 0;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (students[i]!=null) {
                int score = getScoreBySubject(student,subject);
                totalScore += score;
                count++;
            }
        }
        return count == 0 ? 0 : totalScore / count;
    }

    public void sortByTotalScore(){
        for (int i = 0; i < students.length - 1; i++) {
            for (int j = students.length-i-1; j > 0; j--) {
                if (students[j] != null && students[j + 1] != null) {
                    if (students[j].getTotalScore() < students[j + 1].getTotalScore()) {
                        Student temp = students[j];
                        students[j] = students[j+1];
                        students[j + 1] = temp;
                    }
                }
            }
        }
    }

    public Student findStudentById(int studentId){
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && students[i].getStudentId() == studentId) {
                return student;
            }
        }
        return null;
    }

    private int getScoreBySubject(Student student, String subject) {
        switch (subject){
            case "语文" -> {
                return student.getChineseScore();
            }
            case "数学" ->{
                return student.getMathScore();
            }
            case "历史" ->{
                if(student instanceof MiddleStudent middleStudent){//历史物理成绩为中学生特有，用instanceof关键词判断是否为中学生在返回成绩
                    return ((MiddleStudent) student).getHistoryScore();
                }
            }
            case "物理" ->{
                if(student instanceof MiddleStudent middleStudent){
                    return ((MiddleStudent) student).getPhysicsScore();
                }
            }

        }
        return 0;
    }


}
